<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

set_time_limit(600);

class JFX_Module_Content extends JFX_Module{

    public $keyname = 'content';
    public $title = 'JFX Content';
    public $ajaxEnabled = true;
    public $version = '0.2.9';
    public $defaultContent = 'Error-Success Messages go here';
    

    public function __construct(){
        $args = func_get_args();
        eval("parent::__construct('".implode("', '", $args)."');");

        $CONFIG = JFX::registry('config');
        $VIEW = JFX::registry('JFX_View');

		$this->defaultContent = $this->lang('default_content');
		$this->title = $this->lang('module_title');




        //$this->addStyleSheet('default-layouts.css');
        //$VIEW->addIeCssLink($CONFIG->baseUrl.$CONFIG->moduleCssFolder.'/'.$this->keyname.'/ie6-hacks.css');

    }


    public function getBlockActionButtons($blockid, $pageId){
		$CONFIG = JFX::registry('config');
		$SETTINGS = JFX::registry('JFX_Settings');

		$block = JFX_Block::getBlockById($blockid, $pageId);

		$allButtons = array();

        switch($block->moduleAction){
            case 'content':
				if($block->pageid != $SETTINGS->get('template_page_id') || $pageId==$SETTINGS->get('template_page_id')){
					if($this->checkPermission('perm_edit_block')){
						// move up button
						$button1 = new JFX_Block_Button;
						$button1->pageId = $pageId;
						$button1->alternativeText = $this->lang('block_edit_button_alt');
						$button1->moduleName = $this->keyname;
						$button1->moduleMethod = 'updateSettings';
						$button1->imageUrl = $CONFIG->imageUrl.'/admin/icons/file-edit.png';
						$button1->title = $this->lang('block_edit_button_title');
						$button1->blockId = $blockid;
						$allButtons[] = $button1;
					}
				}
			break;
        }

		return $allButtons;

    }




    public function getBlockEditButtons($block){

		$SETTINGS = JFX::registry('JFX_Settings');

		if($block->moduleKey!=$this->keyname){
			return array();
		}

		switch($block->moduleAction){
			case 'content':
				$button1 = new JFX_Block_Button;
				$button1->pageId = $block->pageid;
				$button1->alternativeText = $this->lang('block_edit_button_alt');
				$button1->moduleName = $this->keyname;
				$button1->moduleMethod = 'updateSettings';
				$button1->imageUrl = $this->imageUrl.'/edit.gif';
				$button1->title = $this->lang('block_edit_button_title');
				$button1->blockId = $block->id;
				return array($button1);

			break;
		}
    }


    public function getBlockActions(){
        return array(
            array(
                'keyname' => 'content',
                'title' => $this->lang('block_action_content_title'),
                'description' => ''
            ),
            array(
                'keyname' => 'errorSuccess',
                'title' => $this->lang('block_action_errorSuccess_title'),
                'description' => ''
            )
        );
    }

	public function getContentTypeName($action){
		return $this->lang('block_action_'.$action.'_title');
	}

  

    public function delete($details){
		$CORE = JFX::registry('JFX_Core');
        $CORE->deleteContentLang('heading', $details['id'], '*', $details['workspace_id']);
        $this->deleteContentLang('content', $details['id'], '*', $details['workspace_id']);
    }

    public function prepare($details){
        //$this->resetLanguages();
        return $this->getContent($details['id'], $details['page_id'], $details['workspace_id']);
    }

    public function useCache($details){
        return false;
    }

    public function updateSettings($block){

        if(false === ($block instanceof JFX_Block)){
			
			return;
		}
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
		$CORE = JFX::registry('JFX_Core');
		$SETTINGS = JFX::registry('JFX_Settings');
		$USER = JFX::registry('JFX_User');


        $id = $block->id;

        switch($block->moduleAction){
            case 'content':

				$disableForm = true;

				if(strlen(trim($block->details['template_id']))==0){

					$disableForm = false;

					if(Antz_IntelliForm::submitted($this->keyname.'-edit-settings')){
						// update our settings

						$error = false;

						if(!$error){
							$DB->showErrors(true);
							$DB->debug(true);
							$CORE->updateContentLang('heading', $id, post('heading', '', 500));
							$this->updateContentLang('content', $id, post('content', '', 500000));
							$this->updateContentLang('show_heading', $id, post('show_heading', '', 500000));

							$DB->showErrors(false);
							$DB->debug(false);
							JFX::addSuccess($this->lang('settings_applied'));

						}

					}

				}

                $heading = JFX_Block::getTitleById($id, $block->pageid, '', $USER->workspace);
				$content = $this->getContentLang('content', $id);
				$showHeading = (int) $this->getContentLang('show_heading', $id);


                //if($heading == '') $heading = $this->lang('default_heading');
                //if($content == '') $content = $this->lang('default_content');


                $formFields = array(
                    array(
                        'name' => 'heading',
                        'type' => 'text',
                        'value' => post('heading', $heading, 500),
                        'label' => $this->lang('edit_heading'),
						'disabled' => $disableForm
                    ),
					array(
                        'name' => 'show_heading',
                        'type' => 'checkbox',
						'checked' => (int) $showHeading,
                        'value' => 1,
                        'label' => $this->lang('show_heading'),
						'disabled' => $disableForm
                    ),

                    array(
                        'name' => 'content',
                        'type' => ($disableForm) ? 'textarea' : 'be_tinymce',
                        'value' => post('content', $content, 500000),
                        'id' => $this->keyname.'-edit-content',
                        'label' => $this->lang('edit_content'),
						'disabled' => $disableForm
                    )                    
                );

				if(!$disableForm){
					$formFields[] = array(
                        'name' => 'submitBtn',
                        'type' => 'submit',
                        'value' => $this->lang('edit_submit'),
                        'label' => '&nbsp;'
                    );
				}


                echo JFX::makeRapidForm($formFields, $this->lang('edit_legend'), $this->keyname.'-edit-settings');

            break;
            case 'errorSuccess':
                echo $this->lang('no_actions_defined_error');
            break;

        }


    }

	public function getContentRaw($details){
		$DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $SMARTY = JFX::registry('Smarty');
		$USER = JFX::registry('JFX_User');

		if(is_object($details)){
            $details = JFX::objectToArray($details);
        }

		switch($details['module_action']){

            case 'content':
                $result = $this->getContentLang('content', $details['id'], '', $USER->workspace);
				return $result;

            break;
            case 'errorSuccess':
                $retVal = JFX::getError().JFX::getSuccess();
                if(trim($retVal)=='') return $this->defaultContent;
                else return $retVal;
				
            break;

        }

	}

    public function getContent($blockId, $pageId, $workspaceid=''){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $SMARTY = JFX::registry('Smarty');
		$USER = JFX::registry('JFX_User');

		$block = JFX_Block::getBlockById($blockId, $pageId);

		switch($block->details['module_action']){

            case 'content':

                $heading = JFX_Block::getTitleById($block->details['id'], $block->details['page_id'], '', $USER->workspace);
                $showHeading = $this->getContentLang('show_heading', $block->details['id']);
                $content = $this->getContentRaw($block->details);

                $result = '';
				
				if($showHeading && strlen(trim((string) $heading)) > 0) $result = '<h2 class="'.$this->keyname.'-heading">'.$heading.'</h2>
';
                if(strlen(trim((string) $content)) > 0) $result .= '<div class="'.$this->keyname.'-content">'.$content.'</div>
';
                eval($block->details['php_code']);
                $result = JFX::fetchSmarty($result);
                return $result;

            break;
            case 'errorSuccess':
                return $this->getContentRaw($block->details);
            break;

        }

    }



    public function copy($action, $oldBlockid, $newBlockid, $oldPageid, $newPageid, $oldWorkspaceid, $newWorkspaceid){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
		$CORE = JFX::registry('JFX_Core');

        $installedLangs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}languages");
        foreach($installedLangs as $k=>$v){
            $heading = $CORE->getContentLang('heading', $oldBlockid, $v['keyname'], $oldWorkspaceid);
            $content = $this->getContentLang('content', $oldBlockid, $v['keyname'], $oldWorkspaceid);
            $CORE->updateContentLang('heading', $newBlockid, $heading, '*', $newWorkspaceid);
            $this->updateContentLang('content', $newBlockid, $content, '*', $newWorkspaceid);
        }

    }

    public function copyToNewBlock($oldid, $newid, $oldworkspaceid, $newworkspaceid){
        $DB = JFX::registry('db');
		$CORE = JFX::registry('JFX_Core');
        //$DB->showOutput(true);
        //$DB->showErrors(true);
        $heading = JFX_Block::getTitleById($oldid, '', '', $oldworkspaceid);
        $content = $this->getContentLang('content', $oldid, '', $oldworkspaceid);

        $CORE->updateContentLang('heading', $newid, $heading, '*', $newworkspaceid);
        $this->updateContentLang('content', $newid, $content, '*', $newworkspaceid);
        //$DB->showOutput(false);
        //$DB->showErrors(false);
    }

    public function createDefaultBlock($action, $blockid, $workspaceid){
		$CORE = JFX::registry('JFX_Core');

        switch($action){
            case 'content':
                $heading = $this->lang('default_heading');
                $content = $this->lang('default_content');
                $CORE->updateContentLang('heading', $blockid, $heading, '*', $workspaceid);
                $this->updateContentLang('content', $blockid, $content, '*', $workspaceid);
            break;
        };
    }




    public function getDefaultContent($action){
        switch($action){
            case 'content':
                $title = $this->lang('default_heading');
                $content = $this->lang('default_content');
                return '<h2>'.$title.'</h2>'.$content;
            break;
            case 'errorSuccess':
                return $this->defaultContent;
            break;
        }
    }



    public function install(){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        require('install/install.php');
    }

    public function uninstall(){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        require('install/uninstall.php');
    }
}